package com.example.travelhelper.controller;

import com.example.travelhelper.pojo.WebTitle;
import com.example.travelhelper.service.WebService;
import com.example.travelhelper.service.serviceImpl.PageViewServiceImpl;
import com.example.travelhelper.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@CrossOrigin
@RestController
@RequestMapping("/web")
public class WebController {
    @Autowired
    private WebService webService;
    @Autowired
    private PageViewServiceImpl pageViewService;
    @Value("${Getimagepath}")
    private String finderPath;
    @Value("${imagepath}")
    private String imgfilePath;

    @GetMapping("/foreground")
    public ResponseData disPlayForeground() {
        List<WebTitle> webTitle = webService.disPlay();
        int pageViewCount = pageViewService.getPageView();
        Map<String, Object> map = new HashMap<>();
        map.put("webTitle",webTitle);
        // map.put("pageViewCount",pageViewCount);
        return ResponseData.success("前台展示标题", map);
    }
    //前台进行展示
    @GetMapping("/getLookCount")
    ResponseData lookCount(){
        LocalDate endDate = LocalDate.now();
        LocalDate startDate = endDate.minusDays(7);
        return webService.getlookCount(startDate.toString(),endDate.toString());

    }



    @GetMapping("look")
    ResponseData look(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        String deviceType = "Unknown";
        String os = "Unknown";
        String browser = "Unknown";

        if (userAgent != null) {
            if (userAgent.contains("Mobile")) {
                deviceType = "Mobile";
            } else if (userAgent.contains("Tablet")) {
                deviceType = "Tablet";
            } else {
                deviceType = "Desktop";
            }

            if (userAgent.contains("Windows")) {
                os = "Windows";
            } else if (userAgent.contains("Mac OS")) {
                os = "Mac OS";
            } else if (userAgent.contains("Linux")) {
                os = "Linux";
            } else if (userAgent.contains("Android")) {
                os = "Android";
            } else if (userAgent.contains("iOS")) {
                os = "iOS";
            }

            if (userAgent.contains("Chrome")) {
                browser = "Chrome";
            } else if (userAgent.contains("Firefox")) {
                browser = "Firefox";
            } else if (userAgent.contains("Safari")) {
                browser = "Safari";
            } else if (userAgent.contains("Edge")) {
                browser = "Edge";
            } else if (userAgent.contains("IE")) {
                browser = "Internet Explorer";
            }
        }

        System.out.println("Device Type: " + deviceType);
        System.out.println("Operating System: " + os);
        System.out.println("Browser: " + browser);



        return webService.look(deviceType+":"+os+":"+browser+":"+request.getRemoteAddr());
    }

    //前台展示浏览量
    @GetMapping("/views")
    public ResponseData views(){
        int pageViewCount = pageViewService.getPageView();
        return ResponseData.success("浏览量",pageViewCount);
    }

    //后台进行展示
    @GetMapping("/background")
    public ResponseData disPlayBackground() {
        List<WebTitle> webTitle = webService.disPlay();
        return ResponseData.success("后台展示标题", webTitle);
    }

    //背景文件上传
    @PostMapping("/upload")
    public ResponseData upload(MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseData.fail("上传文件为空！");
        }

        try {
            String filename = file.getOriginalFilename();
            String extension = filename.substring(filename.lastIndexOf("."));
            String newFilename = UUID.randomUUID().toString() + extension;
            String filePath =  imgfilePath + newFilename;

            //获取文件父目录路径
            File directory = new File(filePath).getParentFile();
            if (!directory.exists()) {
                directory.mkdirs();
            }

            //保存文件到静态资源目录
            byte[] bytes = file.getBytes();
            Path path = Paths.get(filePath);
            Files.write(path, bytes);

            return ResponseData.success("上传成功！", finderPath+newFilename);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseData.fail("上传失败！");
        }
    }

    //后台进行修改
    @PostMapping("/update")
    public ResponseData update(@RequestBody WebTitle webTitle) {
        System.out.println(webTitle);
        return webService.update(webTitle);
    }
}
